package cn.ultrarisk.scadmin.manager.biz.common.dao;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.common.entity.Dict;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import org.springframework.stereotype.Repository;

/**
 * 数据字典数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class DictDAO extends BaseDAO<Dict, Integer> {
    /**
     * 构造器
     */
    public DictDAO() {
        super(Dict.class, Dict.COLUMN_PROPERTY_MAP);
    }

    public Page<Dict> findPage(String name, String remark, int pageNo, int pageSize) {
        Query query = new Query().likeFull(Dict.NAME, name).likeFull(Dict.REMARK, remark);
        return findPage(query, pageNo, pageSize);
    }

    public Dict getByName(String name, Integer idNo) {
        return get(Query.query(Dict.NAME, name).neq(Dict.ID, idNo));
    }

    private Dict getByNameAndType(String name, String type) {
        return get(Query.query(Dict.NAME, name).eq(Dict.TYPE, type));
    }

    public Boolean getBooleanValue(String name) {
        Dict dict = getByNameAndType(name, Dict.TYPE_BOOLEAN);
        return dict == null ? null : dict.getBooleanValue();
    }

    public Integer getIntValue(String name) {
        Dict dict = getByNameAndType(name, Dict.TYPE_INT);
        return dict == null ? null : dict.getIntValue();
    }

    public Long getLongValue(String name) {
        Dict dict = getByNameAndType(name, Dict.TYPE_LONG);
        return dict == null ? null : dict.getLongValue();
    }

    public Double getDoubleValue(String name) {
        Dict dict = getByNameAndType(name, Dict.TYPE_DOUBLE);
        return dict == null ? null : dict.getDoubleValue();
    }

    public String getStringValue(String name) {
        Dict dict = getByNameAndType(name, Dict.TYPE_STRING);
        return dict == null ? null : dict.getStringValue();
    }
}
